package com.bingbaihanji.cloud.resp;

import lombok.Data;
import lombok.experimental.Accessors;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author 冰白寒祭
 * @date 2024-03-06 20:48:52
 * @description //TODO
 */

@Data
@Accessors(chain = true)
public class ResultData<T> {
    private String code;
    private String message;
    private T data;
    private String timestamp;

    public ResultData() {
        var date = new Date();
        var simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        this.timestamp = simpleDateFormat.format(date);
    }

    public static <T> ResultData<T> success(T data) {
        var resultData = new ResultData();
        resultData.setCode(ReturnCodeEnum.RC200.getCode()).
                setMessage(ReturnCodeEnum.RC200.getMessage()).
                setData(data);
        return resultData;
    }

    public static <T> ResultData fail(String code) {
        var resultData = new ResultData();
        resultData.setCode(Objects.requireNonNull(ReturnCodeEnum.getReturnCodeEnum(code)).getCode()).
                setMessage(Objects.requireNonNull(ReturnCodeEnum.getReturnCodeEnum(code)).getMessage());
        return resultData;
    }
    public static <T> ResultData fail(String code, String data) {
        var resultData = new ResultData();
        resultData.setCode(Objects.requireNonNull(ReturnCodeEnum.getReturnCodeEnum(code)).getCode()).
                setMessage(Objects.requireNonNull(ReturnCodeEnum.getReturnCodeEnum(code)).getMessage()).
                setData(data);
        return resultData;
    }


}
